package com.example.photodrawai.config;

/**
 * @author hanWang
 * @Title:
 * @Package
 * @Description:
 * @date 2023/8/211:05 下午
 */

import feign.RequestInterceptor;
import feign.RequestTemplate;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Configuration
public class FeignConfig implements RequestInterceptor {
    private static final Logger log = LoggerFactory.getLogger(FeignConfig.class);
    private static final String USER = "user";
    private static final String TOKEN = "token";
    private static final String REQUEST_ID = "X-Request-Id";
    private static final String BUILD_VERSION = "Build-Version";
    private static final String LONGITUDE = "Longitude";
    private static final String LATITUDE = "Latitude";
    private static final String CITY_CODE = "City-Code";
    private static final String DEVICE_ID = "Device-Id";
    private static final String OA_ID = "Oa-Id";
    private static final String OS = "OS";
    private static final String BROWSER = "Browser";
    private static final String CHANNEL = "Channel";
    private static final String OS_VERSION = "OS-Version";
    private static final String API_VERSION = "API-Version";
    private static final String MOBILE_BRAND = "Mobile-Brand";
    private static final String MOBILE_MODEL = "Mobile-Type";
    private static final String NETWORK_ENV = "Network-Env";
    private static final String NETWORK_PROVIDER = "Network-Provider";
    private static final String MEDIA_CHANNEL = "Media-Channel";
    private static final String PKG_DELIVERY_CODE = "Pkg-Delivery-Code";
    private static final String LANGUAGE = "Language";
    private static final String PRODUCT = "Product";
    private static final String CITY_NAME = "City-Name";
    private static final String ANDROID_ID = "Android-Id";
    private static final String USER_AGENT = "User-Agent";
    private static final String BlackBox = "BlackBox";
    private static final String[] ALLOW_HEADERS = new String[]{"user", "token", "X-Request-Id".toLowerCase(), "Build-Version".toLowerCase(), "Longitude".toLowerCase(), "Latitude".toLowerCase(), "City-Code".toLowerCase(), "Device-Id".toLowerCase(), "Oa-Id".toLowerCase(), "OS".toLowerCase(), "Browser".toLowerCase(), "Channel".toLowerCase(), "OS-Version".toLowerCase(), "API-Version".toLowerCase(), "Mobile-Brand".toLowerCase(), "Mobile-Type".toLowerCase(), "Network-Env".toLowerCase(), "Network-Provider".toLowerCase(), "Media-Channel".toLowerCase(), "Pkg-Delivery-Code".toLowerCase(), "Language".toLowerCase(), "Product".toLowerCase(), "City-Name".toLowerCase(), "Android-Id".toLowerCase(), "User-Agent".toLowerCase(), "BlackBox".toLowerCase()};
    private static final Set<String> PASS_HEADERS;

    public FeignConfig() {
    }

    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        if (StringUtils.isNotBlank(MDC.get("X-Request-Id"))) {
            requestTemplate.header("X-Request-Id", new String[]{StringUtils.trimToEmpty(MDC.get("X-Request-Id"))});
        }

        if (StringUtils.isNotBlank(MDC.get("Build-Version"))) {
            requestTemplate.header("Build-Version", new String[]{StringUtils.trimToEmpty(MDC.get("Build-Version"))});
        }

        if (StringUtils.isNotBlank(MDC.get("Longitude"))) {
            requestTemplate.header("Longitude", new String[]{StringUtils.trimToEmpty(MDC.get("Longitude"))});
        }

        if (StringUtils.isNotBlank(MDC.get("Latitude"))) {
            requestTemplate.header("Latitude", new String[]{StringUtils.trimToEmpty(MDC.get("Latitude"))});
        }

        if (StringUtils.isNotBlank(MDC.get("City-Code"))) {
            requestTemplate.header("City-Code", new String[]{StringUtils.trimToEmpty(MDC.get("City-Code"))});
        }

        if (StringUtils.isNotBlank(MDC.get("Oa-Id"))) {
            requestTemplate.header("Oa-Id", new String[]{StringUtils.trimToEmpty(MDC.get("Oa-Id"))});
        }

        if (StringUtils.isNotBlank(MDC.get("OS"))) {
            requestTemplate.header("OS", new String[]{StringUtils.trimToEmpty(MDC.get("OS"))});
        }

        if (attributes == null) {
            log.warn("requestTemplate attributes is null");
        } else {
            HttpServletRequest request = attributes.getRequest();
            Enumeration<String> headerNames = request.getHeaderNames();
            if (headerNames != null) {
                try {
                    while(headerNames.hasMoreElements()) {
                        String key = (String)headerNames.nextElement();
                        String value = request.getHeader(key);
                        if (PASS_HEADERS.contains(key.toLowerCase()) && value != null) {
                            requestTemplate.header(key, new String[]{value});
                        }
                    }
                } catch (Exception var7) {
                }

            }
        }
    }

    static {
        PASS_HEADERS = new HashSet(Arrays.asList(ALLOW_HEADERS));
    }
}
